﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QuickApp.Core
{
    /// <summary>
    /// 对SearchModel做为Action参数的绑定
    /// </summary>
    public class PageModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = (PageInfo)(bindingContext.Model ?? new PageInfo());
            var dict = controllerContext.HttpContext.Request.Params;

            int page;
            if (int.TryParse(dict["page"], out page))
            {
                model.PageIndex = page - 1;
            }

            int rows;
            if (int.TryParse(dict["rows"], out rows))
            {
                model.PageSize = rows;
            }

            string sort = dict["sort"];
            if (!string.IsNullOrEmpty(sort))
            {
                model.SortName = sort;
            }

            string order = dict["order"];
            if (!string.IsNullOrEmpty(order))
            {
                model.SortOrder = order;
            }

            return model;
        }
    }
}
